<!--
 * Copyright (c) 2013-Now http://jeesite.com All rights reserved.
 * No deletion without permission, or be held responsible to law.
 * @author YDZ
-->
<template>
  <BasicDrawer
    v-bind="$attrs"
    :showFooter="true"
    :okAuth="'design:change:pmDesignchange:edit'"
    @register="registerDrawer"
    @ok="handleSubmit"
    :showOkBtn="false"
    width="50%"
  >
    <template #title>
      <Icon :icon="getTitle.icon" class="pr-1 m-1" />
      <span> {{ getTitle.value }} </span>
    </template>
    <BasicForm @register="registerForm" />
  </BasicDrawer>
</template>
<script lang="ts">
  export default defineComponent({
    name: 'ViewsDesignChangePmDesignchangeForm',
  });
</script>
<script lang="ts" setup>
  import { defineComponent, ref, computed } from 'vue';
  import { useI18n } from '/@/hooks/web/useI18n';
  import { useMessage } from '/@/hooks/web/useMessage';
  import { router } from '/@/router';
  import { Icon } from '/@/components/Icon';
  import { BasicForm, FormSchema, useForm } from '/@/components/Form';
  import { BasicDrawer, useDrawerInner } from '/@/components/Drawer';
  import { PmDesignchange, pmDesignchangeSave, pmDesignchangeForm } from '/@/api/design/change/pmDesignchange';

  const emit = defineEmits(['success', 'register']);

  const { t } = useI18n('design.change.pmDesignchange');
  const { showMessage } = useMessage();
  const record = ref<PmDesignchange>({} as PmDesignchange);
  const getTitle = computed(() => ({
    icon: router.currentRoute.value.meta.icon || 'ant-design:book-outlined',
    value: record.value.isNewRecord ? t('新增设计变更') : t('查看设计变更'),
  }));

  const inputFormSchemas: FormSchema[] = [
    {
      label: t('工程编码'),
      field: 'projectcode',
      component: 'Input',
      componentProps: {
        maxlength: 50,
      },
      dynamicDisabled: true,
    },
    {
      label: t('工程名称'),
      field: 'projectname',
      component: 'Input',
      componentProps: {
        maxlength: 500,
      },
      dynamicDisabled: true,
    },
    {
      label: t('变更编号'),
      field: 'designchangeno',
      component: 'Input',
      componentProps: {
        maxlength: 50,
      },
      dynamicDisabled: true,
    },
    {
      label: t('洽商单编码'),
      field: 'negotiationcode',
      component: 'Input',
      componentProps: {
        maxlength: 50,
      },
      dynamicDisabled: true,
    },
    {
      label: t('申请时间'),
      field: 'createDate',
      component: 'DatePicker',
      componentProps: {
        format: 'YYYY-MM-DD HH:mm:ss',
        // showTime: { format: 'HH:mm' },
      },
      dynamicDisabled: true,
    },
    {
      label: t('确认日期'),
      field: 'ddate',
      component: 'DatePicker',
      componentProps: {
        format: 'YYYY-MM-DD HH:mm:ss',
        // showTime: { format: 'HH:mm' },
      },
      dynamicDisabled: true,
    },
    {
      label: t('是否含水表'),
      field: 'isinvolvewatermeter',
      component: 'Select',
      componentProps: {
        dictType: 'sys_yes_no',
        allowClear: true,
      },
      dynamicDisabled: true,
      // rules: [{ pattern: /^\d+$/, message: t('请输入一个正整数') }],
    },
    {
      label: t('变更原因类型'),
      field: 'changesettingid',
      component: 'Select',
      componentProps: {
        dictType: 'zf_designChg_reson_type',
        allowClear: true,
      },
      dynamicDisabled: true,
    },
    {
      label: t('变更原因'),
      field: 'changeremark',
      component: 'InputTextArea',
      componentProps: {
        maxlength: 500,
      },
      dynamicDisabled: true,
      colProps: { lg: 24, md: 24 },
    },

    {
      label: t('洽商退回人'),
      field: 'enduser',
      component: 'Input',
      componentProps: {
        maxlength: 50,
      },
      dynamicDisabled: true,
    },
    {
      label: t('洽商退回消息'),
      field: 'msg',
      component: 'Input',
      componentProps: {
        maxlength: 2000,
      },
      dynamicDisabled: true,
      // colProps: { lg: 24, md: 24 },
    },

    // {
    //   label: t('设计变更ID'),
    //   field: 'designchangecode',
    //   component: 'Input',
    //   componentProps: {
    //     maxlength: 50,
    //   },
    // },
   
    // {
    //   label: t('材料变更情况'),
    //   field: 'materialchange',
    //   component: 'Input',
    //   componentProps: {
    //     maxlength: 255,
    //   },
    // },
    // {
    //   label: t('预算变更情况'),
    //   field: 'budgetchange',
    //   component: 'Input',
    //   componentProps: {
    //     maxlength: 50,
    //   },
    // },
    // {
    //   label: t('合同价变更情况'),
    //   field: 'contractpricechange',
    //   component: 'Input',
    //   componentProps: {
    //     maxlength: 50,
    //   },
    // },
   
    // {
    //   label: t('变更附件地址'),
    //   field: 'fysfilepath',
    //   component: 'Input',
    //   componentProps: {
    //     maxlength: 500,
    //   },
    // },
    // {
    //   label: t('cdcano'),
    //   field: 'cdcano',
    //   component: 'Input',
    //   componentProps: {
    //     maxlength: 50,
    //   },
    // },
   
    
    // {
    //   label: t('fysfileid'),
    //   field: 'fysfileid',
    //   component: 'Input',
    //   componentProps: {
    //     maxlength: 50,
    //   },
    // },
    // {
    //   label: t('变更类型'),
    //   field: 'designerchangetype',
    //   component: 'Input',
    //   componentProps: {
    //     maxlength: 8,
    //   },
    //   rules: [{ pattern: /^\d+$/, message: t('请输入一个正整数') }],
    // },
   
   
    // {
    //   label: t('预算金额'),
    //   field: 'budgetprice',
    //   component: 'Input',
    //   componentProps: {
    //     maxlength: 16,
    //   },
    //   rules: [{ pattern: /^(?:-?\d+|-?\d{1,3}(?:,\d{3})+)?(?:\.\d+)?$/, message: t('请输入一个数值') }],
    // },
    // {
    //   label: t('预算备注'),
    //   field: 'budgetremarks',
    //   component: 'Input',
    //   componentProps: {
    //     maxlength: -1,
    //   },
    // },
    // {
    //   label: t('附件上传'),
    //   field: 'dataMap',
    //   component: 'Upload',
    //   componentProps: {
    //     loadTime: computed(() => record.value.__t),
    //     bizKey: computed(() => record.value.id),
    //     bizType: 'pmDesignchange_file',
    //     uploadType: 'all',
    //   },
    //   colProps: { lg: 24, md: 24 },
    // },
  ];

  const [registerForm, { resetFields, setFieldsValue, validate }] = useForm({
    labelWidth: 120,
    schemas: inputFormSchemas,
    dynamicDisabled: true,
    baseColProps: { lg: 24, md: 24 },
  });

  const [registerDrawer, { setDrawerProps, closeDrawer }] = useDrawerInner(async (data) => {
    setDrawerProps({ loading: true });
    await resetFields();
    const res = await pmDesignchangeForm(data);
    record.value = (res.pmDesignchange || {}) as PmDesignchange;
    record.value.__t = new Date().getTime();
    if(record.value.isinvolvewatermeter == 0  || record.value.isinvolvewatermeter == 1){
      record.value.isinvolvewatermeter = record.value.isinvolvewatermeter + ''
    }
    setFieldsValue(record.value);
    setDrawerProps({ loading: false });
  });

  async function handleSubmit() {
    try {
      const data = await validate();
      setDrawerProps({ confirmLoading: true });
      const params: any = {
        isNewRecord: record.value.isNewRecord,
        id: record.value.id,
      };
      // console.log('submit', params, data, record);
      const res = await pmDesignchangeSave(params, data);
      showMessage(res.message);
      setTimeout(closeDrawer);
      emit('success', data);
    } catch (error: any) {
      if (error && error.errorFields) {
        showMessage(t('您填写的信息有误，请根据提示修正。'));
      }
      console.log('error', error);
    } finally {
      setDrawerProps({ confirmLoading: false });
    }
  }
</script>
